<template>
	<view class="login_content">
	</view>
</template>
<script>
import GLOBAL_VARIABLE from "../helmet/global_variable.js";
export default {
  data() {
    return {
      username: "",
      timer: null,
      password: "",
      disable: true,
      checked: false,
      pkey: null
    };
  },
  watch: {
    "$store.state.admin_info"(val) {
      this.adminInfo = val;
      this.UA();
    }
  },
  //生命周期函数
  onLoad: function(option) {
    // console.log(option);
    this.helmetpassword=option.helmetpassword
    this.helmetusername=option.helmetusername
    this.search=option.search
       console.log(this.search);
      if(this.search ==undefined){

      }else if(this.search==true){
        this.search=false
        location.reload();
      }
    // this.helmetpassword = "123456";
    // this.helmetusername = "shyq";
  },
  onShow() {
    uni.showLoading({
      title: "加载中"
    });
    URL='https://iotplat.yunzhen.tech:8083/runde_api/'
    // URL='api/'

    if (this.helmetusername) {
      this.login();
    } else {
      uni.hideLoading();
      uni.showToast({
        title: "请前往“我的”页面进行远程信息配置！",
        icon: "none",
        duration: 2100000
      });
    }
  },

  onReady() {},
  beforeDestroy() {
    clearInterval(this.timer);
    this.timer = null;
    uni.hideLoading();
  },
  destroyed() {
    clearInterval(this.timer);
    this.timer = null;
    uni.hideLoading();
  },
  onHide() {
    clearInterval(this.timer);
    window.clearTimeout(this.timer);
    this.timer = null;
    uni.hideLoading();
  },
  //自定义返回
  onBackPress: function() {
    uni.getSystemInfo({
      success: function(res) {
        // 判断为安卓的手机
        if (res.platform == "android") {
          // 安卓退出app
          plus.runtime.quit();
        } else {
          // 判断为ios的手机，退出App
          plus.ios
            .import("UIApplication")
            .sharedApplication()
            .performSelector("exit");
        }
      }
    });
    return true;
  },
  beforeDestroy() {
    clearInterval(this.timer);
    this.timer = null;
  },
  destroyed() {
    clearInterval(this.timer);
    this.timer = null;
  },
  methods: {
    change() {
      this.checked = !this.checked;
      // console.log(this.checked);
    },
    login() {
      uni.showLoading({
        title: "加载中",
        mask: true
      });
      const data = {
        username: this.helmetusername,
        password: this.helmetpassword
      };
      console.log(data);
      let _this = this;
      // https://iotplat.yunzhen.tech:8083/runde_api/
      uni.request({
        url:
          URL+"index.php?ctl=tool&act=get_pkey", //仅为示例，并非真实接口地址。
        data: {
          user_name: data.username,
          pwd: data.password
        },
        header: {
          "content-type": "application/json" //自定义请求头信息
        },
        method: "POST",
        success: res => {
          console.log("获取pkey成功");
          console.log(res.data);
          if (res.data.status == false) {
            uni.showToast({
              title: res.data.msg + "请前往“我的”页面重新进行远程信息配置！",
              icon: "none",
              duration: 2100000
            });
            uni.hideLoading();
          } else {
            console.log(res.data);
            this.pkey = res.data.data;
            // console.log(this.pkey);
            this.text = "request success";
            _this.pkeyget(data.username);
          }
        },
        fail: res => {
          uni.showToast({
            title: "服务器出错，正在重新加载中...",
            icon: "none",
            duration: 2100000
          });
        }
      });
      this.timer = setInterval(() => {
        uni.hideLoading();
        location.reload();
      }, 3000);

    },
    // 获取taken
    pkeyget(name) {
      uni.request({
        url:
          URL+"index.php?ctl=tool&act=get_token",
        data: {
          user_name: name,
          pkey: this.pkey
        },
        header: {
          "content-type": "application/json" //自定义请求头信息
        },
        method: "POST",
        success: res => {
          console.log("获取token成功" + res.data.status);
          if (res.data.status) {
            this.session_id = res.data.session_id;
            let data = {
              act: "ma_login",
              access_token: res.data.session_id,
              user_name: name
            };
            this.$store.dispatch("WEBSOCKET_SEND", JSON.stringify(data));
          } else {
            uni.showToast({
              title: "接口出错，正在重新登录中,请稍等！",
              icon: "none",
              duration: 2100000
            });
            uni.hideLoading();
          }
          // console.log(res.data.status);
        },
        fail: res => {
          uni.showToast({
            title: "服务器出错，正在重新加载中...",
            icon: "none",
            duration: 2100000
          });
        }
      });
    },
    UA() {
      let _this = this;
      var user = _this.adminInfo.admin_info.sip_info.sip_id;
      var host = _this.adminInfo.admin_info.sip_info.sip_host;
      var wss = _this.adminInfo.admin_info.sip_info.wss_url;
      // console.log( _this.adminInfo)
      let Config = {
        uri: user + "@" + host,
        transportOptions: {
          wsServers: [wss]
        },
        authorizationUser: _this.adminInfo.admin_info.sip_info.sip_id,
        password: _this.adminInfo.admin_info.sip_info.sip_pwd,
        sessionDescriptionHandlerFactoryOptions: {
          peerConnectionOptions: {
            rtcConfiguration: {
              iceServers: [
                {
                  urls: "stun:" + _this.adminInfo.admin_info.sip_info.stun_host
                },
                {
                  urls: "turn:" + _this.adminInfo.admin_info.sip_info.turn_host,
                  username: _this.adminInfo.admin_info.sip_info.turn_user,
                  credential: _this.adminInfo.admin_info.sip_info.turn_pwd
                }
              ]
            }
          }
        }
      };
      try {
        _this.sipObj = new SIP.UA(Config);
      } catch (ex) {
        uni.hideLoading();
        return;
      }
      GLOBAL_VARIABLE.HOST = _this.sipObj;
      // 监听成功
      _this.sipObj.on("registered", () => {
        console.log("registered ok", "监听成功");
        // 获取组信息
        // https://iotplat.yunzhen.tech:8083/runde_api/
        uni.request({
          url:
            URL+"index.php?ctl=group&act=get_group_list", //仅为示例，并非真实接口地址。
          data: {
            admin_id: _this.adminInfo.admin_info.admin_id
          },
          header: {
            "content-type": "application/json" //自定义请求头信息
          },
          method: "POST",
          success: res => {
            console.log(res.data.msg);
            if (res.data.msg == "获取成功！") {
              this.text = "request success";
              uni.redirectTo({
                url:
                  `/pages/helmet/toukuizu?admin_id=` +
                  _this.adminInfo.admin_info.admin_id +
                  "&host=" +
                  host +
                  "&back=" +
                  false+'&sip_id='+_this.adminInfo.admin_info.sip_info.sip_host
              });
            } else {
              uni.showToast({
                title: "服务器出错，正在重新登录中...",
                icon: "none",
                duration: 2100000
              });
              uni.hideLoading();
            }
          },
          fail: res => {
            uni.showToast({
              title: "服务器出错，正在重新加载中...",
              icon: "none",
              duration: 2100000
            });
          }
        });
      });
      // 监听失败
      _this.sipObj.on("registrationFailed", (response, cause) => {
        console.log("registrationFailed, ", response, cause);
        uni.showToast({
          title: "登录出错，正在重新登录中...",
          icon: "none",
          duration: 2100000
        });
        uni.hideLoading();
      });
    }
  }
};
</script>


<style lang="scss" scoped>
// /deep/ .uni-page-head {
//     background: linear-gradient(-34deg, #1040be, #026dff)  !important;
// }
.login_content {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: url(../../static/L3RX2Eyy9p.jpg) no-repeat;
  background-position: center center;
  background-size: 135% 100%;
  background-color: #f6f6f6;
  .basket {
    text-align: center;
    width: 75%;
    // background-color:transparent;
    // background: rgba($color: #000000, $alpha: 1.0);
    background: white;
    // height: 60%;
    margin-top: 37%;
    padding: 30rpx 70rpx;
    border-radius: 46rpx;
    position: relative;
    // opacity: .5;
  }
  .logoimage {
    // display: flex;
    // top: 0rpx;
    position: absolute;
    top: -9%;
    left: 35%;
    background-color: #ffffff;
    border-radius: 46rpx;
    width: 180rpx;
    height: 180rpx;
    // margin: -24% 0rpx -13% 30%;
    box-shadow: 0rpx 24rpx 40rpx #e6e8ff;
  }
}

.appTitle {
  font-size: 50rpx;
  font-weight: 400;
  text-align: center;
  margin: 28% 0rpx 35rpx 0rpx;
}
.status_bar {
  height: var(--status-bar-height);
  width: 100%;
}
.login_input {
  // margin: 15rpx;
  display: flex;
  justify-content: start;
  align-items: center;
  border-bottom: 1px solid #cacaca;
  padding: 15rpx 15rpx;
  margin: 20rpx;

  input {
    height: 60rpx;
    color: #999;
    padding-left: 30rpx;
  }
}
.username {
  margin-top: 30rpx;
}
.login_btn {
  // margin-bottom: 1%;
}

.login_btn button {
  width: 100%;
  height: 77rpx;
  color: #f8f5f5;
  background: url(../../static/images/denglubeijing.png) no-repeat;
  background-size: 600rpx 77rpx;
  border-radius: 50rpx;
  font-size: 22rpx;
  padding: 5rpx 0;
  margin-top: 5%;
  margin-bottom: 10%;
}
.logo {
  width: 90%;
  height: 70%;
  margin: 15% 0px 0px 5%;
}
.denglulogo {
  width: 1080rpx;
}
.rember {
  margin-top: 6%;
  padding: 0rpx 16rpx;
}
</style>
